package com.serkol.ams.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.module.sys.entity.SysRoleMenu;
import com.serkol.ams.module.sys.service.SysRoleMenuService;
import com.serkol.ams.module.sys.mapper.SysRoleMenuMapper;
import com.serkol.ams.module.sys.valid.SysRoleMenuParams;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Hesen
* @description 针对表【sys_role_menu(角色权限表)】的数据库操作Service实现
* @createDate 2024-05-27 19:04:34
*/
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
    implements SysRoleMenuService{

    @Override
    public boolean editFenpei(SysRoleMenuParams data) {
       LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId,data.getRoleId());
        if(this.baseMapper.delete(wrapper) > 0){
            for (Object menuId : data.getMenuIds()) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(data.getRoleId());
                sysRoleMenu.setMenuId(Long.parseLong(menuId.toString()));
                this.baseMapper.insert(sysRoleMenu);
            }
            return true;
        }
        return false;
    }

    @Override
    public List<Long> finByRoleId(long roleId) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId,roleId);
        List<SysRoleMenu> sysRoleMenus = this.baseMapper.selectList(wrapper);
        if(sysRoleMenus.size() > 0){
            return sysRoleMenus.stream().map(SysRoleMenu::getMenuId).toList();
        }
        return List.of();
    }
}




